package com.anywhere.freightman.order.service.impl;

import com.anywhere.freightman.common.BaseMapper;
import com.anywhere.freightman.common.BaseService;
import com.anywhere.freightman.order.dao.WaybillLogDao;
import com.anywhere.freightman.order.model.WaybillLog;
import com.anywhere.freightman.order.service.IWaybillLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.Weekend;
import tk.mybatis.mapper.weekend.WeekendCriteria;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class WaybillLogService extends BaseService<WaybillLog> implements IWaybillLogService {

    @Autowired
    private WaybillLogDao waybillLogDao;

    @Override
    public BaseMapper<WaybillLog> getBaseDao() {
        return waybillLogDao;
    }

    @Override
    public Example getExample(WaybillLog entity) {
        Weekend<WaybillLog> weekend = Weekend.of(WaybillLog.class);
        WeekendCriteria<WaybillLog, Object> criteria = weekend.weekendCriteria();

        if (entity == null) {
            return weekend;
        }

        if (entity.getOrderNo() != null) {
            criteria.andEqualTo("orderNo", entity.getOrderNo());
        }

        return weekend;
    }

    @Override
    public void insert(String orderNo, String content) {
        WaybillLog model = new WaybillLog();
        model.setOrderNo(orderNo);
        model.setContent(content);
        model.setEventDate(new Date());
        waybillLogDao.insert(model);
    }

    @Override
    public List<WaybillLog> getListByOrderNo(String orderNo) {
        return waybillLogDao.getListByOrderNo(orderNo);
    }
}
